/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/
#include <QProcess>
#include <QSettings>
#include <QApplication>
#include "lansatorul.h"

Lansatorul::Lansatorul(QObject * parent)
		: QSystemTrayIcon(parent)
{
	setIcon(QIcon(":/images/lansatorul.svg"));
}

void Lansatorul::start()
{
	bool startAll=false;
	QString application;

	QString serverStartUrl;
	QString serverStartArgs;

	QString serverStopUrl;

	QString serverStopArgs;

	QString programUrl;
	QString programArgs;
	QSettings s;

	const QStringList & args=qApp->arguments();

	if (args.indexOf("--start")!=-1)
		startAll=true;

	if (args.indexOf("--application")!=-1 && args.size()>args.indexOf("--application")+1)
		application=args[args.indexOf("--application")+1];

	s.beginGroup( application );

	if (args.indexOf("--serverStartURL")!=-1 && args.size()>args.indexOf("--serverStartURL")+1)
		s.setValue("serverStartUrl",args[args.indexOf("--serverStartURL")+1]);
	else
		serverStartUrl=s.value("serverStartUrl" , "").toString();

	if (args.indexOf("--serverStartARGS")!=-1 && args.size()>args.indexOf("--serverStartARGS")+1)
		s.setValue("serverStartArgs",args[args.indexOf("--serverStartARGS")+1]);
	else
		serverStartArgs=s.value( "serverStartArgs" , "" ).toString();
		

	if (args.indexOf("--serverStopURL")!=-1 && args.size()>args.indexOf("--serverStopURL")+1)
		s.setValue("serverStopUrl",args[args.indexOf("--serverStopURL")+1]);
	else
		serverStopUrl=s.value( "serverStopUrl" , "" ).toString();

	if (args.indexOf("--serverStopARGS")!=-1 && args.size()>args.indexOf("--serverStopARGS")+1)
		s.setValue("serverStopArgs",args[args.indexOf("--serverStopARGS")+1]);
	else
		serverStopArgs=s.value( "serverStopArgs" , "" ).toString();

	if (args.indexOf("--programURL")!=-1 && args.size()>args.indexOf("--programURL")+1)
		s.setValue("programUrl",args[args.indexOf("--programURL")+1]);
	else
		programUrl=s.value( "programUrl" , "" ).toString();

	if (args.indexOf("--programARGS")!=-1 && args.size()>args.indexOf("--programARGS")+1)
		s.setValue("programArgs",args[args.indexOf("--programARGS")+1]);
	else
		programArgs=s.value( "programArgs" , "" ).toString();

	s.endGroup();
	if (!startAll)
		return;

	showMessage( "Lansatorul", "Pornesc serverul, va rog asteptati ...");
	QProcess ss(this);
	ss.start( serverStartUrl+" "+serverStartArgs);
	if (!ss.waitForStarted())
	{
		showMessage( "Lansatorul", "Nu pot porni serverul", QSystemTrayIcon::Critical);
		return;
	}

	showMessage( "Lansatorul", "Serverul a pornit cu success.\nPornesc aplicatia.");
	QProcess as(this);
	as.start( programUrl+" "+programArgs);
	if (!as.waitForStarted())
	{
		showMessage( "Lansatorul", "Nu pot porni aplicatia", QSystemTrayIcon::Critical);
		return;
	}
	while (!as.waitForFinished(500)) // wait application to quit
		qApp->processEvents();

	showMessage( "Lansatorul", "Aplicatia s-a oprit.\nOpresc serverul va rog asteptati ...");

	QProcess sp(this);
	sp.start( serverStopUrl+" "+serverStopArgs);
	if (!sp.waitForStarted())
	{
		showMessage( "Lansatorul", "Nu pot porni serverul", QSystemTrayIcon::Critical);
		return;
	}
	while (!sp.waitForFinished(500)) // wait for server to quit
		qApp->processEvents();

	showMessage( "Lansatorul", "Serverul s-a oprit cu success");
}

Lansatorul::~Lansatorul()
{
}
